package com.blogsoho.taurus.services.impl;

import com.blogsoho.taurus.dao.mapper.CustomerMapper;
import com.blogsoho.taurus.services.CustomerService;
import com.blogsoho.taurus.vm.ResponsePagerWrapper;
import com.blogsoho.taurus.vm.ResponseWrapper;
import com.blogsoho.taurus.vo.CustomerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 16978 on 2017/6/11.
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ResponseWrapper<?> addCustomer(CustomerVO customer) {
        boolean result = customerMapper.addCustomer(customer) > 0 ? true : false;
        ResponseWrapper<?> response = new ResponseWrapper<>();
        response.setSuccess(result);
        return response;
    }

    @Override
    public ResponsePagerWrapper<List<CustomerVO>> getAllCustomer(int start, int length) {
        ResponsePagerWrapper<List<CustomerVO>> response = new ResponsePagerWrapper<>();
        List<CustomerVO> customers = customerMapper.getAllCustomer(start, length);
        int countCustomer = customerMapper.getCountCustomer();
        response.setSuccess(true);
        response.setData(customers);
        response.setRecordsFiltered(countCustomer);
        response.setRecordsTotal(countCustomer);
        return response;
    }

    @Override
    public CustomerVO getCustomerById(int id) {
        CustomerVO customer = customerMapper.getCustomerById(id);

        return customer;
    }

    @Override
    public ResponseWrapper<?> updateCustomer(CustomerVO customer) {
        boolean result = customerMapper.updateCustomer(customer) > 0 ? true : false;
        ResponseWrapper<?> response = new ResponseWrapper<>();
        response.setSuccess(result);
        return response;
    }

    @Override
    public ResponsePagerWrapper<List<CustomerVO>> getAllCustomerByAlertTime(int day) {
        ResponsePagerWrapper<List<CustomerVO>> response = new ResponsePagerWrapper<>();
        List<CustomerVO> customers = customerMapper.getCustomerByAlertTime(day);
        int countCustomer = customerMapper.getCountCustomer();
        response.setSuccess(true);
        response.setData(customers);
        response.setRecordsFiltered(countCustomer);
        response.setRecordsTotal(countCustomer);
        return response;
    }
}
